package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.system.dto.UserRoleDto;
import com.yc.sole.system.entity.UserRole;
import com.yc.sole.system.mapper.UserRoleMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class UserRoleRepository extends ServiceImpl<UserRoleMapper, UserRole> {

    /**
     * 批量新增
     *
     * @param userRoleList
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchUserRole(List<UserRoleDto> userRoleList) {
        List<UserRole> userRoles = BeanUtil.copyToList(userRoleList, UserRole.class);
        this.saveBatch(userRoles);
    }

    /**
     * 根据用户id删除
     *
     * @param userIds
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserRoleByUserIds(Collection<Long> userIds) {
        LambdaQueryWrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery().in(UserRole::getUserId, userIds);
        this.remove(wrapper);
    }

    /**
     * 根据角色id删除
     *
     * @param roleIds
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserRoleByRoleId(Collection<Long> roleIds) {
        LambdaQueryWrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery().in(UserRole::getRoleId, roleIds);
        this.remove(wrapper);
    }

    /**
     * 根据用户id查询用户的角色id
     *
     * @param userId
     * @return
     */
    public List<Long> selectRoleIdsByUserId(Long userId) {
        LambdaQueryWrapper<UserRole> userRoleWrapper = Wrappers.<UserRole>lambdaQuery()
                .select(UserRole::getRoleId)
                .eq(UserRole::getUserId, userId);
        List<UserRole> userRoleList = this.list(userRoleWrapper);
        if (CollUtil.isEmpty(userRoleList)) {
            return Collections.emptyList();
        }
        return userRoleList.stream().map(UserRole::getRoleId).toList();
    }

    /**
     * 根据用户id查询用户的角色id
     *
     * @param userIds
     * @return
     */
    public List<UserRoleDto> selectRoleIdsByUserIds(Collection<Long> userIds) {
        LambdaQueryWrapper<UserRole> userRoleWrapper = Wrappers.<UserRole>lambdaQuery()
                .select(UserRole::getUserId, UserRole::getRoleId)
                .in(UserRole::getUserId, userIds);
        List<UserRole> userRoleList = this.list(userRoleWrapper);
        if (CollUtil.isEmpty(userRoleList)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(userRoleList, UserRoleDto.class);
    }

    /**
     * 根据角色id查询用户id
     *
     * @param roleIds
     * @return
     */
    public Set<Long> selectUserIdsByRoleIds(Collection<Long> roleIds) {
        LambdaQueryWrapper<UserRole> userRoleWrapper = Wrappers.<UserRole>lambdaQuery()
                .select(UserRole::getUserId)
                .in(UserRole::getRoleId, roleIds);
        List<UserRole> userRoleList = this.list(userRoleWrapper);
        if (CollUtil.isEmpty(userRoleList)) {
            return Collections.emptySet();
        }
        return userRoleList.stream().map(UserRole::getUserId).collect(Collectors.toSet());
    }
}
